import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { getLoginUserUsingPost } from '@/service/api/yonghujiekou.ts'

/**
 * 登录用户信息
 */
export const useLoginUserStore = defineStore('loginUser', () => {
  const loginUser = ref<API.LoginUserVo>({
    userName: "未登录",
    id: 0
  })

  async function fetchLoginUser() {
    //todo 调用后端接口获取用户信息
    var res = await getLoginUserUsingPost()
    if (res.data.code === 0 && res.data.data != null) {
      loginUser.value = res.data.data
    }
  }

  /**
   * 设置登录用户信息
   * @param userName
   */
  function setLoginUser(userName: string) {
    loginUser.value.userName = userName
  }

  return {
    loginUser,
    fetchLoginUser,
    setLoginUser
  }
})
